/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F240
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F280.h>
#include "KB_Code.h"
#include "KB_API_Pos.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/

const uint8_t keyboard_api_pos_tab[168]=
{
//col_0,
KEY_API_ESCAPE,
KEY_API_GAT,
KEY_API_TAB,
KEY_API_CAPLOCK,
KEY_API_LEFTSHIFT,
KEY_API_LEFTCTRL,
KEY_API_REC,
KEY_API_REC,

//col_1,
KEY_API_REC,
KEY_API_1,
KEY_API_Q,
KEY_API_A,
KEY_API_K45,
KEY_API_LEFTGUI,
KEY_API_REC,
KEY_API_REC,

//col_2,
KEY_API_F1,
KEY_API_2,
KEY_API_W,
KEY_API_S,
KEY_API_Z,
KEY_API_LEFTALT,
KEY_API_REC,
KEY_API_REC,

//col_3,
KEY_API_F2,
KEY_API_3,
KEY_API_E,
KEY_API_D,
KEY_API_X,
KEY_API_REC,
KEY_API_REC,
KEY_API_REC,

//col_4,
KEY_API_F3,
KEY_API_4,
KEY_API_R,
KEY_API_F,
KEY_API_C,
KEY_API_REC,
KEY_API_REC,
KEY_API_REC,

//col_5,
KEY_API_F4,
KEY_API_5,
KEY_API_T,
KEY_API_G,
KEY_API_V,
KEY_API_REC,
KEY_API_REC,
KEY_API_REC,

//col_6,
KEY_API_F5,
KEY_API_6,
KEY_API_Y,
KEY_API_H,
KEY_API_B,
KEY_API_SPACEBAR,
KEY_API_REC,
KEY_API_REC,

//col_7,
KEY_API_F6,
KEY_API_7,
KEY_API_U,
KEY_API_J,
KEY_API_N, 
KEY_API_REC,
KEY_API_REC,
KEY_API_REC,

//col_8,
KEY_API_F7,
KEY_API_8,
KEY_API_I,
KEY_API_K,
KEY_API_M,
KEY_API_REC,
KEY_API_REC,
KEY_API_REC,

//col_9,
KEY_API_F8,
KEY_API_9,
KEY_API_O,
KEY_API_L,
KEY_API_CMA,
KEY_API_RIGHTALT,
KEY_API_REC,
KEY_API_REC,

//col_10,
KEY_API_F9,
KEY_API_0,
KEY_API_P,
KEY_API_COL,
KEY_API_DOT,
KEY_API_FN,
KEY_API_REC,
KEY_API_REC,

//col_11,
KEY_API_F10,
KEY_API_MIS,
KEY_API_OQO,
KEY_API_CC,
KEY_API_SL,
KEY_API_RIGHTCTRL,
KEY_API_REC,
KEY_API_REC,

//col_12,
KEY_API_F11,
KEY_API_EQU,
KEY_API_EQO,
KEY_API_K42,
KEY_API_RIGHTSHIFT,
KEY_API_LEFT,
KEY_API_REC,
KEY_API_REC,

//col_13,
KEY_API_F12,
KEY_API_BACKSPACE,
KEY_API_K29,
KEY_API_ENTER,
KEY_API_UP,
KEY_API_DOWN,
KEY_API_REC,
KEY_API_REC,

//col_14,
KEY_API_DELETE,
KEY_API_INSERT,
KEY_API_PAGEUP,
KEY_API_PAGEDOWN,
KEY_API_REC,
KEY_API_RIGHT,
KEY_API_REC,
KEY_API_REC,

//col_15,
KEY_API_REC,
KEY_API_NUMLOCK,
KEY_API_PAD7,
KEY_API_PAD4,
KEY_API_PAD1,
KEY_API_PAD0,
KEY_API_REC,
KEY_API_REC,

//col_16,
KEY_API_REC,
KEY_API_PADNSL,
KEY_API_PAD8,
KEY_API_PAD5,
KEY_API_PAD2,
KEY_API_REC,
KEY_API_REC,
KEY_API_REC,

//col_17,
KEY_API_REC,
KEY_API_PADNMU,
KEY_API_PAD9,
KEY_API_PAD6,
KEY_API_PAD3,
KEY_API_PADDEL,
KEY_API_REC,
KEY_API_REC,

//col_18,
KEY_API_REC,
KEY_API_PADNMI,
KEY_API_PADNPL,
KEY_API_REC,
KEY_API_PADENTER,
KEY_API_REC,
KEY_API_REC,
KEY_API_REC, 

};

const uint8_t keyboard_usage_tab[168]=
{
//col_0,
KEY_ESCAPE,
KEY_GAT,
KEY_TAB,
KEY_CAPLOCK,
KEY_LEFTSHIFT,
KEY_LEFTCTRL,
KEY_REC,
KEY_REC,

//col_1,
KEY_REC,
KEY_1,
KEY_Q,
KEY_A,
KEY_K45,
KEY_LEFTGUI,
KEY_REC,
KEY_REC,

//col_2,
KEY_F1,
KEY_2,
KEY_W,
KEY_S,
KEY_Z,
KEY_LEFTALT,
KEY_REC,
KEY_REC,

//col_3,
KEY_F2,
KEY_3,
KEY_E,
KEY_D,
KEY_X,
KEY_REC,
KEY_REC,
KEY_REC,

//col_4,
KEY_F3,
KEY_4,
KEY_R,
KEY_F,
KEY_C,
KEY_REC,
KEY_REC,
KEY_REC,

//col_5,
KEY_F4,
KEY_5,
KEY_T,
KEY_G,
KEY_V,
KEY_REC,
KEY_REC,
KEY_REC,

//col_6,
KEY_F5,
KEY_6,
KEY_Y,
KEY_H,
KEY_B,
KEY_SPACEBAR,
KEY_REC,
KEY_REC,

//col_7,
KEY_F6,
KEY_7,
KEY_U,
KEY_J,
KEY_N, 
KEY_REC,
KEY_REC,
KEY_REC,

//col_8,
KEY_F7,
KEY_8,
KEY_I,
KEY_K,
KEY_M,
KEY_REC,
KEY_REC,
KEY_REC,

//col_9,
KEY_F8,
KEY_9,
KEY_O,
KEY_L,
KEY_CMA,
KEY_RIGHTALT,
KEY_REC,
KEY_REC,

//col_10,
KEY_F9,
KEY_0,
KEY_P,
KEY_COL,
KEY_DOT,
KEY_FN,
KEY_REC,
KEY_REC,

//col_11,
KEY_F10,
KEY_MIS,
KEY_OQO,
KEY_CC,
KEY_SL,
KEY_RIGHTCTRL,
KEY_REC,
KEY_REC,

//col_12,
KEY_F11,
KEY_EQU,
KEY_EQO,
KEY_K42,
KEY_RIGHTSHIFT,
KEY_LEFT,
KEY_REC,
KEY_REC,

//col_13,
KEY_F12,
KEY_BACKSPACE,
KEY_K29,
KEY_ENTER,
KEY_UP,
KEY_DOWN,
KEY_REC,
KEY_REC,

//col_14,
KEY_DELETE,
KEY_INSERT,
KEY_PAGEUP,
KEY_PAGEDOWN,
KEY_REC,
KEY_RIGHT,
KEY_REC,
KEY_REC,

//col_15,
KEY_WHEEL,
KEY_NUMLOCK,
KEY_PAD7,
KEY_PAD4,
KEY_PAD1,
KEY_PAD0,
KEY_REC,
KEY_REC,

//col_16,
KEY_REC,
KEY_PADNSL,
KEY_PAD8,
KEY_PAD5,
KEY_PAD2,
KEY_REC,
KEY_REC,
KEY_REC,

//col_17,
KEY_REC,
KEY_PADNMU,
KEY_PAD9,
KEY_PAD6,
KEY_PAD3,
KEY_PADDEL,
KEY_REC,
KEY_REC,

//col_18,
KEY_REC,
KEY_PADNMI,
KEY_PADNPL,
KEY_REC,
KEY_PADENTER,
KEY_REC,
KEY_REC,
KEY_REC, 
};

const uint8_t Keyboard_USB_Custom_Table1[][2]=
{
	KEY_K29,KEY_LED_TEST,
	KEY_ESCAPE,KEY_RESET,
	KEY_LEFTGUI,KEY_WinLock,

	KEY_F1,CSMKEY_DARKEN_INDEX,
	KEY_F2,CSMKEY_BRIGHT_INDEX,
	KEY_F3,KEY_WIN_TAB,
	KEY_F4,CSMKEY_ALBROWSER_INDE,
	KEY_F5,CSMKEY_ACSEARCH_INDEX,
	KEY_F6,KEY_WIN_SHIFT_S,
	KEY_F7,CSMKEY_PTRACK_INDEX,
	KEY_F8,CSMKEY_PLAYPAUSE_INDEX,
	KEY_F9,CSMKEY_NTRACK_INDEX,
	KEY_F10,CSMKEY_MUTE_INDEX,
	KEY_F11,CSMKEY_VOLDEC_INDEX,
	KEY_F12,CSMKEY_VOLINC_INDEX,
	
	KEY_PAGEUP,KEY_OLED_SWITCH,
	KEY_PAGEDOWN,KEY_LCD_OFF,	
	
	KEY_UP,		    KEY_LED_BRIGHT,
	KEY_DOWN,		  KEY_LED_DARK,
	KEY_LEFT,		  KEY_LED_SLOW,
	KEY_RIGHT,		  KEY_LED_QUICK,
  
	KEY_Q,			KEY_BT_DEV1,
	KEY_W,			KEY_BT_DEV2,  
	KEY_E,			KEY_BT_DEV3,
	KEY_R,			KEY_RF_SWITCH,
	
	KEY_0,			KEY_PRINT,
	KEY_MIS,		KEY_SCROLL,	
	KEY_EQU,		KEY_PAUSE,
  KEY_OQO,    KEY_HOME,
  KEY_EQO,    KEY_END,
	
	KEY_SPACEBAR,KEY_LOCK,
};
const uint16_t Keyboard_USB_COMB_TableSize = (sizeof(Keyboard_USB_Custom_Table1)/2);

const uint8_t Keyboard_USB_MAC_Custom_Table1[][2]=
{
	KEY_K29,KEY_LED_TEST,
	KEY_ESCAPE,KEY_RESET,
	KEY_LEFTGUI,KEY_LEFTGUI,
	
	KEY_F1,KEY_F1,
	KEY_F2,KEY_F2,
	KEY_F3,KEY_F3,
	KEY_F4,KEY_F4,
	KEY_F5,KEY_F5,
	KEY_F6,KEY_F6,
	KEY_F7,KEY_F7,
	KEY_F8,KEY_F8,
	KEY_F9,KEY_F9,
	KEY_F10,KEY_F10,
	KEY_F11,KEY_F11,
	KEY_F12,KEY_F12,
	
	KEY_PAGEUP,KEY_OLED_SWITCH,
	KEY_PAGEDOWN,KEY_LCD_OFF,
	
	KEY_UP,		    KEY_LED_BRIGHT,
	KEY_DOWN,		  KEY_LED_DARK,
	KEY_LEFT,		  KEY_LED_SLOW,
	KEY_RIGHT,		  KEY_LED_QUICK,
  
	KEY_Q,			KEY_BT_DEV1,
	KEY_W,			KEY_BT_DEV2,  
	KEY_E,			KEY_BT_DEV3,
	KEY_R,			KEY_RF_SWITCH,

	KEY_0,			KEY_PRINT,
	KEY_MIS,		KEY_SCROLL,	
	KEY_EQU,		KEY_PAUSE,
  KEY_OQO,    KEY_HOME,
  KEY_EQO,    KEY_END,
	
	KEY_SPACEBAR,KEY_LOCK,
};

const uint8_t Keyboard_USB_ANDR_Custom_Table1[][2]=
{
	KEY_K29,KEY_LED_TEST,
	KEY_ESCAPE,KEY_RESET,
	KEY_LEFTGUI,KEY_WinLock,
	
//	KEY_DELETE,KEY_LED_SWITCH,
	
	KEY_F1,CSMKEY_DARKEN_INDEX,
	KEY_F2,CSMKEY_BRIGHT_INDEX,
	KEY_F3,KEY_WIN_TAB,
	KEY_F4,KEY_WIN_E,
	KEY_F5,CSMKEY_ACSEARCH_INDEX,
	KEY_F6,KEY_WIN_SHIFT_S,
	KEY_F7,CSMKEY_PTRACK_INDEX,
	KEY_F8,CSMKEY_PLAYPAUSE_INDEX,
	KEY_F9,CSMKEY_NTRACK_INDEX,
	KEY_F10,CSMKEY_MUTE_INDEX,
	KEY_F11,CSMKEY_VOLDEC_INDEX,
	KEY_F12,CSMKEY_VOLINC_INDEX,
	
//	KEY_RIGHTALT,KEY_OLED_LIGHT,
//	KEY_RIGHTCTRL,KEY_OLED_SPEED,
	KEY_HOME,KEY_OLED_SWITCH,
//	KEY_SL,KEY_OLED_COLOUR_SWITCH,
	
	KEY_LEFT,		  KEY_LCD_P,
	KEY_RIGHT,		KEY_LCD_N,
	KEY_ENTER,		KEY_LCD_E,
  
	KEY_Q,			KEY_RF_SWITCH,
	KEY_W,			KEY_BT_DEV1,
	KEY_E,			KEY_BT_DEV2,
	KEY_R,			KEY_BT_DEV3,
};

const uint8_t Keyboard_USB_IOS_Custom_Table1[][2]=
{
	KEY_K29,KEY_LED_TEST,
	KEY_ESCAPE,KEY_RESET,
	KEY_LEFTGUI,KEY_WinLock,
	
//	KEY_DELETE,KEY_LED_SWITCH,
	
	KEY_F1,KEY_F1,
	KEY_F2,KEY_F2,
	KEY_F3,KEY_F3,
	KEY_F4,KEY_F4,
	KEY_F5,KEY_F5,
	KEY_F6,KEY_F6,
	KEY_F7,KEY_F7,
	KEY_F8,KEY_F8,
	KEY_F9,KEY_F9,
	KEY_F10,KEY_F10,
	KEY_F11,KEY_F11,
	KEY_F12,KEY_F12,
	
	KEY_HOME,KEY_OLED_SWITCH,
	
	KEY_LEFT,		  KEY_LCD_P,
	KEY_RIGHT,		KEY_LCD_N,
	KEY_ENTER,		KEY_LCD_E,
  
	KEY_Q,			KEY_RF_SWITCH,
	KEY_W,			KEY_BT_DEV1,
	KEY_E,			KEY_BT_DEV2,
	KEY_R,			KEY_BT_DEV3
};

const uint8_t Keyboard_USB_WASD_Table1[8][2]=
{
	#if	1
	KEY_W,KEY_UP,
	KEY_A,KEY_LEFT,
	KEY_S,KEY_DOWN,
	KEY_D,KEY_RIGHT,
	KEY_UP,KEY_W,
	KEY_LEFT,KEY_A,
	KEY_DOWN,KEY_S,
	KEY_RIGHT,KEY_D,	
	#else
	KEY_W,KEY_W,
	KEY_A,KEY_A,
	KEY_S,KEY_S,
	KEY_D,KEY_D,
	KEY_UP,KEY_UP,
	KEY_LEFT,KEY_LEFT,
	KEY_DOWN,KEY_DOWN,
	KEY_RIGHT,KEY_RIGHT,
	#endif
};

const uint16_t Keyboard_USB_Consumer_Table[32]=
{
	CSMKEY_NTRACK,			// (0c b5) scan next track
	CSMKEY_PTRACK,			// (0c b6) scan previous track
	CSMKEY_STOP,				// (0c b7) stop
	CSMKEY_PLAYPAUSE, 	// (0c cd) play/pause
	CSMKEY_VOLUME,			// (0c e0) Volume
	CSMKEY_MUTE,   			// (0c e2) mute
	CSMKEY_BASS,				// (0c e3) bass
	CSMKEY_TREBLE,			// (0c e4) treble
	CSMKEY_BASSBOOST,		// (0c e5) bass boost
	CSMKEY_VOLINC,  		// (0c e9) volume increment
	CSMKEY_VOLDEC,  		// (0c ea) volume decrement
	CSMKEY_BASSINC, 		// (0c 152) bass increment
	CSMKEY_BASSDEC,  		// (0c 153) bass decrement
	CSMKEY_TREBLEINC,		// (0c 154) treble increment
	CSMKEY_TREBLEDEC,		// (0c 155) treble decrement
	CSMKEY_ALEMAIL,			// (0c 18a) al email reader
	CSMKEY_ACSEARCH,		// (0c 221) ac search
	CSMKEY_ACHOME,			// (0c 223) ac home (??)
	CSMKEY_ACBACK,   		// (0c 224) ac back
	CSMKEY_ACFORWARD,		// (0c 225) ac forward
	CSMKEY_ACSTOP,			// (0c 226) ac stop
	CSMKEY_ACREFRESH,		// (0c 227) ac refresh
	CSMKEY_ACBOOKMARK,	// (0c 22a) ac bookmarks (????)
	CSMKEY_ALCALCULATOR,// (0c 192) al calculator (???)
	CSMKEY_ALBROWSER,		// (0c 194) al browser (????)
	CSMKEY_ALCONSUMER,	// (0c 183) al consumer control configuration (?????)	
	CSMKEY_ACOPEN,			// (0c 202) ac open
	CSMKEY_ACCLOSE,			// (0c 203) ac close
	CSMKEY_ACSAVE,			// (0c 207) ac save
	CSMKEY_ACUSER,			// (0c 218) ac user
	CSMKEY_BRIGHT,			// (0c 21a) ac undo
	CSMKEY_DARKEN,			// (0c b8) Eject		
};

const uint8_t Mask_Table[8]=
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,};

const uint32_t Mask32_Table[32]=
{1<<0,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7,
 1<<8,1<<9,1<<10,1<<11,1<<12,1<<13,1<<14,1<<15,
 1<<16,1<<17,1<<18,1<<19,1<<20,1<<21,1<<22,1<<23,
 //1<<24,1<<25,1<<26,1<<27,1<<28,1<<29,1<<30,1<<31
};


const uint8_t Sim_KeyTab_WinScrShot[] = 
{
	KEY_LEFTGUI,		1,
	KEY_LEFTSHIFT,		1,
	KEY_S,				1,
	KEY_S,				0,
	KEY_LEFTSHIFT,		0,
	KEY_LEFTGUI,		0,
};
const uint8_t Sim_KeyTab_WinScrShot_LEN = sizeof(Sim_KeyTab_WinScrShot);


const uint8_t Sim_KeyTab_MacSearch[] = 
{
	KEY_FN,				0,
	KEY_LEFTGUI,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		0,
	KEY_LEFTGUI,		0,
};
const uint8_t Sim_KeyTab_MacSearch_LEN = sizeof(Sim_KeyTab_MacSearch);


const uint8_t Sim_KeyTab_MacScrShot[] = 
{
	KEY_FN,				0,
	KEY_LEFTGUI,		1,
	KEY_LEFTSHIFT,		1,
	KEY_4,				1,
	KEY_4,				0,
	KEY_LEFTSHIFT,		0,
	KEY_LEFTGUI,		0,
};
const uint8_t Sim_KeyTab_MacScrShot_LEN = sizeof(Sim_KeyTab_MacScrShot);


const uint8_t Sim_KeyTab_MacSiri[] = 
{
	KEY_FN,				0,
	KEY_LEFTGUI,		1,
	KEY_SPACEBAR,		1,
	
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	KEY_SPACEBAR,		1,
	
	KEY_SPACEBAR,		0,
	KEY_LEFTGUI,		0,
};
const uint8_t Sim_KeyTab_MacSiri_LEN = sizeof(Sim_KeyTab_MacSiri);